#pragma once
#include "Point.h"
#include <vector>
using namespace std;
class Snake {
	private:
		typedef Point* Body;
		bool alive = true;
		unsigned length = 10;
		vector<Point> snake;
		Body head;
	public:
		Snake();
		vector<Point> get_snake();
		void up();
		void down();
		void left();
		void right();
		void increase();
		bool isEatten(const Point& F);
		bool isAlive(unsigned width,unsigned height);
};
Snake::Snake()
{
	head = new Point;
	head->X = 9;
	head->Y = 20;
	Body p = head;
	int i = 0;
	snake.push_back(*p);
	while (i < length-1)
	{
		Body body = new Point;
		body->X = p->X;
		body->Y = p->Y - 1;
		p->next = body;
		p = body;
		this->snake.push_back(*body);
		++i;
	}
	
	
}
vector<Point> Snake::get_snake() 
{
	return this->snake;
}
void Snake::up()
{
	Body p = head->next;
	int i = 0;
	while (p!= nullptr)
	{
		int tempX= snake[i].X;
		p->X = tempX;
		int tempY = snake[i].Y;
		p->Y = tempY;
		p = p->next;
		++i;
	}
	cout << endl;
	this->snake.clear();
	head->X--;
	p = head;
	while (p != nullptr)
	{
		this->snake.push_back(*p);
		p = p->next;
	}
}

void Snake::down()
{
	Body p = head->next;
	int i = 0;
	while (p != nullptr)
	{
		int tempX = snake[i].X;
		p->X = tempX;
		int tempY = snake[i].Y;
		p->Y = tempY;
		p = p->next;
		++i;
	}
	cout << endl;
	this->snake.clear();
	head->X++;
	p = head;
	while (p != nullptr)
	{
		this->snake.push_back(*p);
		p = p->next;
	}
}

void Snake::left()
{
	Body p = head->next;
	int i = 0;
	while (p != nullptr)
	{
		int tempX = snake[i].X;
		p->X = tempX;
		int tempY = snake[i].Y;
		p->Y = tempY;
		p = p->next;
		++i;
	}
	cout << endl;
	this->snake.clear();
	head->Y--;
	p = head;
	while (p != nullptr)
	{
		this->snake.push_back(*p);
		p = p->next;
	}
}

void Snake::right()
{
	Body p = head->next;
	int i = 0;
	while (p != nullptr)
	{
		int tempX = snake[i].X;
		p->X = tempX;
		int tempY = snake[i].Y;
		p->Y = tempY;
		p = p->next;
		++i;
	}
	cout << endl;
	this->snake.clear();
	head->Y++;
	p = head;
	while (p != nullptr)
	{
		this->snake.push_back(*p);
		p = p->next;
	}
}

void Snake::increase()
{
	Body p = head;
	while (p->next != nullptr)
		p = p->next;
	Body s = new Point;
	s->X = p->X;
	s->Y = p->Y - 1;
	p->next = s;
	snake.push_back(*s);
	length++;
}

bool Snake::isEatten(const Point& F)
{
	if (head->X == F.X&&head->Y == F.Y)
		return true;
	else
		return false;
}

bool Snake::isAlive(unsigned width, unsigned height)
{
	if (head->X == 0 || head->X == width - 1 || head->Y == 0 || head->Y == height - 1)
		alive = false;
	Body p = head->next;
	while (p != nullptr)
	{
		if (p->X == head->X&&p->Y == head->Y)
			alive = false;
		p = p->next;
	}
	return alive;
}

